import User from './User.js';
import Util from '../Util.js';

document.addEventListener('DOMContentLoaded', () => {

    document.getElementById('submit').addEventListener('click', () => {
        const userNameElem = document.getElementById('userName');
        const userName = userNameElem.value;
        const passwordElem = document.getElementById('password');
        const password = passwordElem.value;
        const signin = document.getElementById('signin').checked;

        // if (signin) {
            
        // } else {
            if (signin || (userName && password)) {
                new User(userName, password, signin).login((user) => {
                    if (!user.loginStatus) {
                        Util.globalMessage('用户名或密码错误或当前用户已在线！', 'error');
                    } else {
                        // console.log(user);
                        window.localStorage.user = JSON.stringify({ name: user.name });
                        window.location.href = '/';
                    }
                });
            } else {
                Util.globalMessage('用户名或密码不能为空！', 'error');
            }
        // }

    })
})